package cn.itcast.gateway.filters;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author mr.wu
 * @Date 2025/1/3 16:06
 */
//@Component
@Order(-1)
public class AuthorizeFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.获取请求实例参数
        MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
        //2.获取QueryString请求参数
        String authorization = queryParams.getFirst("authorization");
        if ("admin".equals(authorization)) {
            //放行到下一个过滤器链
            return chain.filter(exchange);
        }
        //拦截,报403
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.FORBIDDEN);
        return response.setComplete();
    }
}
